import string

from django.shortcuts import render, redirect

"""TODO:??"""
from blueapps.account import get_user_model

from django.http import HttpResponse

from django.conf import settings

from .models import FirstappUser, UserInf

"""
TODO：View ：
使用
这个地方是通过request =》通过url与 

"""


def home(request):
    res = judge_user_and_jump_to_register(request)
    if type(res) is HttpResponse:
        return res

    return render(request, "homepage.html")


def show_user(request):
    res = judge_user_and_jump_to_register(request)
    if type(res) is HttpResponse:
        return res

    user_id = request.user.id

    user = FirstappUser.objects.get(user_id=user_id)
    user_inf = UserInf.objects.filter(user=user)
    return render(request, 'user.html', {'user': user, "statuses": user_inf})


def show_user_inf(request):
    res = judge_user_and_jump_to_register(request)

    if type(res) is HttpResponse:
        return res

    user = FirstappUser.objects.get(user_id=request.user.id)
    user_inf = UserInf.objects.filter(user=user)

    return render(request, "status.html", {'statuses': user_inf})


"""
发送新的记录，并跳转网址
"""


def submit_post(request):
    res = judge_user_and_jump_to_register(request)
    if type(res) is HttpResponse:
        return res

    user = FirstappUser.objects.get(user_id=request.user.id)
    text = request.POST.get('text')
    if text:
        status = UserInf(user=user, text=text)
        status.save()
        return redirect(f'{MyTool.get_path_differ_env()}/user')
    return render(request, 'my_post.html')


def set_super_user(request):
    res = judge_user_and_jump_to_register(request)
    if type(res) is HttpResponse:
        return res

    # 从本地的一个表中获取本地设置的管理员
    # 并将其设置成super_user
    user = request.user
    for name in settings.INIT_SUPERUSER:
        user.objects.update_or_create(username=name,
                                      defaults={'is_superuser': True, 'is_staff': True, 'is_active': True})
    return HttpResponse('已经设置成超级用户')


"""如果第一次进入的时候发现数据库中没有这个对象，就会跳转到registe界面，进行用户初始化"""


def user_register(request):
    user = request.user
    user_name = request.user.username

    email = request.GET.get('email')
    motto = request.GET.get('motto')
    pic_path = request.GET.get('pic')
    region = request.GET.get('region')

    if email and motto and pic_path and region:
        new_user = FirstappUser(
            user=user, email=email, motto=motto, pic=pic_path, region=region)
        new_user.save()
        # 保存完成之后就跳转到/user
        return redirect(f'{MyTool.get_path_differ_env()}/user')

    return render(request, 'register.html', {'user_name': user_name})


def judge_user_and_jump_to_register(request):
    try:
        user = request.user
        user_obj = FirstappUser.objects.get(user=user)
        return None
    except:
        return user_register(request)


"""使用工具"""


class MyTool:
    @staticmethod
    def get_path_differ_env() -> string:
        if settings.ENVIRONMENT == 'dev':
            return ''
        elif settings.ENVIRONMENT == 'stag':
            return f'/stag--{settings.APP_CODE}/'
        else:
            return f'/{settings.APP_CODE}/'
